package org.edu.modules.logistics.base.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.common.base.BaseService;
import org.edu.common.dto.ExcelPo;
import org.edu.common.dto.ImportPo;
import org.edu.common.util.SpringContextUtils;
import org.edu.common.utils.PoiUtils;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = {"基础的文件导入"})
@RestController
@RequestMapping("/veHqExcel")
@Slf4j
public class VeHqExcel {



    @ApiOperation("导出数据）")
    @PostMapping("/exportTemp")
    public Result<?> export(@RequestBody ImportPo importPo, HttpServletResponse resp) {
        List<ExcelPo> list = new ArrayList<>();
        for (String page1 :importPo.getTemp() ) {
            list.add(new ExcelPo(page1,"name"));
        }
        PoiUtils.downloadFile(resp,new ArrayList<>(),list,importPo.getTempMsg());
        return Result.OK();
    }


    @ApiOperation("文件导入）")
    @RequestMapping("/importExcel")
    public Result<?> importExcel(@RequestParam(value = "file", required = false) MultipartFile file,
         String implName,HttpServletRequest request) {
        if(implName==null || !implName.contains(".")){
            return Result.error("接口未配置");
        }
        String[] arr= implName.split("\\.");

        try {
            ImportParams params = new ImportParams();
            params.setTitleRows(0);
            params.setHeadRows(0);
            params.setNeedSave(true);

            ExcelReader excelReader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> list = excelReader.readAll();
            BaseService bean = SpringContextUtils.getBean(arr[0], BaseService.class);
            bean.importExcel(list);
           /* Method method = bean.getClass().getMethod(arr[1],List.class);
            method.invoke(list);*/
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("文件读取失败");
        }
        return Result.OK();
    }


}
